var express = require("express");
var router = express.Router();
var multer = require('multer');
var fs = require('fs');
var upload = multer({
  "dest": "upimgs"
});
var single = upload.single("image");


router.get('/', function(req, res){
  res.render('admin_index');
});
var images = new Map(require('./upimgs.json'));
//产品
router.get('/products', function(req, res){
  // let products = new Map(require('./upimgs.json'));
  // res.locals.products = products;
  // console.log("product="+products.size);
  let set = [];
  images.forEach(function(value,key,map){
    set.push(value);
    console.log("value="+value.file.filename);
  });
  res.send(set);
});

router.get('/admin_product_create', function(req, res){
  res.locals.product = null;
  res.locals.id = null;
  res.render('admin_product');
});

router.get('/img/:filename', function(req, res){
  fs.createReadStream("upimgs/"+req.params.filename).pipe(res);
});


// var images = new Map();
router.post('/upload', single, function(req, res){
  const {
    name,
    information,
    price
  }=req.body;
  const file = req.file;
  const product = {
    file,
    name,
    information,
    price
  };
  // images.push(req.file.filename, product);
  // images[req.file.filename]=product;
  images.set(req.file.filename,product);
  fs.writeFileSync(__dirname+"/upimgs.json", JSON.stringify(Array.from(images)));
  // res.send(req.file);
  res.render('admin_index');
});

router.get('/product/:id', function(req, res){
  // let images = new Map(require('./upimgs.json'));
  const id = req.params.id;
  const product = images.get(id);
  res.locals.id = id;
  res.locals.product = product;
  res.render('admin_product');
});

router.get('/delete/:id', function(req, res){
  // let images = new Map(require('./upimgs.json'));
  const id = req.params.id;
  images.delete(id);
  fs.writeFileSync(__dirname+"/upimgs.json", JSON.stringify(Array.from(images)));
  console.log("ok");
  res.redirect('back');
});

// function checkImg(req, res, next){
//   if(!req.file){
//     const id = req.params.id;
//     req.file = images.get(id).file;
//     console.log("no image");
//   }
//   else {
//     single();
//     console.log("image");
//   }
//   next();
// }

router.post('/update/:id', single, function(req, res){
  // let images = new Map(require('./upimgs.json'));
  const id = req.params.id;
  const {
    name,
    information,
    price
  }= req.body;
  let file = req.file;
  if (!file) {
    file = images.get(id).file;
  }
  console.log(file.filename);
  const product ={
    file,
    name,
    information,
    price
  }
  images.set(id, product);
  fs.writeFileSync(__dirname+"/upimgs.json", JSON.stringify(Array.from(images)));
  res.render('admin_index.ejs');
});


//文章

var articles = require('./article.json');
router.get('/articles', function(req, res){
    // res.locals.articles = articles;
    res.send(articles);
});

router.get('/createarticle', function(req, res){
  res.locals.article = null;
  res.locals.id = null;
    res.render('admin_article');
});

router.get('/article/:id', function(req, res){
  const id = req.params.id;
  const article = articles[id];
  res.locals.article = article;
  res.render('admin_article');
});

router.post('/artupload', function(req, res){
  const {
    title,
    text,
    author
  } = req.body;
  var id =null;
  if (articles.length==0) {
    id = 0;
  }else{
    id = articles[articles.length-1].id+1;
  }

  const article = {
    "id":id,
    "title":title,
    "text":text,
    "author":author,
    "createTime":new Date().toLocaleDateString(),
    "updateTime":new Date().toLocaleDateString()
  }
  articles[id] = article
  fs.writeFileSync(__dirname+"/article.json", JSON.stringify(articles));
  res.send(article);
});

router.get('/artdelete/:id', function(req, res){
  const id = req.params.id;
  delete articles[id];
  res.render('admin_index');
});

router.post('/artupdate/:id', function(req, res){
  const {
    id,
    title,
    text,
    author
  }=req.body;
  articles[Number(id)].title = title;
  articles[Number(id)].text = text;
  articles[Number(id)].author = author;
  articles[Number(id)].updateTime = new Date().toLocaleDateString();
  fs.writeFileSync(__dirname+"/article.json", JSON.stringify(articles));
  res.render('admin_index');
});


module.exports = router;
